import util from "../utils/util.js"
const baseRequest = (opts) => {
	let baseDefaultOpts = {
			url: opts.url,
			header: {'content-type':'application/json;charset=UTF-8'},
			method: opts.method,
			data: opts.data,
		};
	let promise = new Promise(function(resolve, reject) {
		uni.request(baseDefaultOpts).then((res) => {
			if(res[1].data.code=='200'){
				resolve(res[1])
			}else{
				util.page_show_toast(res[1].data.message,2000)
				return false
			}
		}).catch((response) => {
			uni.hideLoading();
			reject(response)
			uni.getNetworkType({
				success: function (res) {
					if(res.networkType=='none'){
						util.page_show_toast('网络正在路上，请检查您的网络',2000)
					}else{
						util.page_show_toast('请求超时,请稍后再试',2000)
					}
				}
			})
		})
	})
	return promise	
};
const tokenRequest = (opts) => {
	let token=uni.getStorageSync("token")?uni.getStorageSync("token"):'';
	let baseDefaultOpts = {
			url: opts.url,
			header: {
				'content-type':'application/json;charset=UTF-8',
				'x-access-token':token,
				'x-idempotent-token': opts.x_token?opts.x_token:''
			},
			method: opts.method,
			data: opts.data,
		};
	let promise = new Promise(function(resolve, reject) {
		uni.request(baseDefaultOpts).then((res) => {
			if(res[1].data.code=='200'){
				resolve(res[1])
			}else{
				util.page_show_toast(res[1].data.message,2000)
				return false
			}
		}).catch((response) => {
			uni.hideLoading();
			reject(response)
			uni.getNetworkType({
				success: function (res) {
					if(res.networkType=='none'){
						util.page_show_toast('网络正在路上，请检查您的网络',2000)
					}else{
						util.page_show_toast('请求超时,请稍后再试',2000)
					}
				}
			})
		})
	})
	return promise	
};


module.exports = {
	baseRequest,
	tokenRequest
}